package chenji.dailykit.commandlisteners;

import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import chenji.dailykit.Main;

/**
 * @author ChenJi Copyright (c) 2016 ChenJi. All rights reserved.
 */
public class CommandItemEditor implements CommandExecutor {

	@Override
	public boolean onCommand(CommandSender arg0, Command arg1, String arg2, String[] arg3) {
		if (!(arg0 instanceof Player)) {
			arg0.sendMessage("§c对不起，控制台不能使用这个指令。");
			return true;
		}
		Player p = (Player) arg0;
		if (!(p.hasPermission("DailyKit.itemeditor.use"))) {
			arg0.sendMessage("§c你没有权限这样做。- DailyKit.itemeditor.use");
			return true;
		}
		if (arg3.length == 0) {
			arg0.sendMessage("§c--------------------§aDailyKit §bItemEditor§c--------------------");
			arg0.sendMessage("§c/ItemEditor edit §a-编辑手中的物品。");
			arg0.sendMessage("§c/ItemEditor openInv §a-打开缓存中的编辑器。");
			arg0.sendMessage("§c/ItemEditor remove §a-移除缓存中的编辑器。");
			arg0.sendMessage("§c-----------------------------------------------------------");
		}
		if (arg3.length == 1) {
			if (arg3[0].equalsIgnoreCase("edit")) {
				if (Main.getItemEditorManager().getItemEditor(p) != null) {
					arg0.sendMessage("§a[ItemEditor] §c你必须将你缓存中的编辑器删除后才可以编辑新的物品。");
					arg0.sendMessage("§a[ItemEditor] §a如需打开缓存中的编辑器，请输入§c/ItemEditor openInv");
					return true;
				}
				if (p.getInventory().getItemInMainHand() == null) {
					arg0.sendMessage("§a[ItemEditor] §c无法编辑空气方块");
					return true;
				}
				if (p.getInventory().getItemInMainHand().getType().equals(Material.AIR)) {
					arg0.sendMessage("§a[ItemEditor] §c无法编辑空气方块");
					return true;
				}
				ItemStack i = p.getInventory().getItemInMainHand();
				Main.getItemEditorManager().openEditor(p, i.clone());
				Main.getItemEditorManager().openEditorInventory(p);
			}
			if (arg3[0].equalsIgnoreCase("openInv")) {
				if (!(Main.getItemEditorManager().openEditorInventory(p))) {
					arg0.sendMessage("§a[ItemEditor] §c你没有缓存中的编辑器。");
					arg0.sendMessage("§a[ItemEditor] §a如需编辑一个物品，请手持物品输入§c/ItemEditor edit");
				}
				return true;
			}
			if (arg3[0].equalsIgnoreCase("remove")) {
				Main.getItemEditorManager().removeEditor(p);
			}
		}
		return true;
	}

}
